//
//  LoginViewController.m
//  SameWorld
//
//  Created by 李小朋 on 2017/3/24.
//  Copyright © 2017年 杨旭. All rights reserved.
//  

#import "LoginViewController.h"
#import "LoginView.h"
#import "BsTenantsViewController.h"
#import "HomeViewController.h"
#import "UserInfo.h"
#import "DynamicLoginViewController.h"
@interface LoginViewController ()<UITextFieldDelegate>

@property (nonatomic ,strong) UserInfo *userInfo;

@property (nonatomic ,strong) LoginView *loginView;


@end

@implementation LoginViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _userInfo = [UserInfo sharedUserInfo];
    }
    return self;
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUI];
  
}

- (void)loadDataLogin {
    
    // http://118.190.98.64/tctxAPI/shop_login.aspx?yhm=18652281519&mm=liuyao0308
    [self.loginView.activityView startAnimating];
    NSString *userName = self.loginView.userName.text;
    NSString *userPassword = self.loginView.userPassword.text;
    NSDictionary *paramDic = @{@"yhm":userName,@"mm":userPassword};
    NSString *string = [NSString stringWithFormat:@"%@/shop_login.aspx",serverUrl];
    [NetworkManager requestStatusPOSTWithURLStr:string paramDic:paramDic finish:^(id responseObject) {
//        NSLog(@"请求成功%@",responseObject);
        NSDictionary *dict = [NSDictionary dictionaryWithDictionary:responseObject];
        if ([[dict objectForKey:@"state"] integerValue] == 1) {  // 登录成功
            
            // 保存用户信息
            NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
            [userDef setObject:self.loginView.userName.text forKey:@"userName"];
            [userDef setObject:self.loginView.userPassword.text forKey:@"userPassword"];
            
            NSDictionary *dataDic = [dict objectForKey:@"data"];
            NSDictionary *data_userDic = [dataDic objectForKey:@"data_user"];
            NSDictionary *data_dealDic = [dataDic objectForKey:@"data_deal"];
            
            _userInfo.isLogin = YES;
            
            _userInfo = [UserInfo mj_objectWithKeyValues:data_userDic];
            _userInfo = [UserInfo mj_objectWithKeyValues:data_dealDic];
            
            HomeViewController *homeVC = [[HomeViewController alloc] init];
            
            [self.navigationController pushViewController:homeVC animated:YES];
            
        }else if ([[dict objectForKey:@"state"]integerValue] == 0) {  // 密码错误
            [self showAlertWithTitle:[dict objectForKey:@"message"]];
        }
        
        [self.loginView.activityView stopAnimating];

    } enError:^(NSError *error) {
         NSLog(@"请求失败%@",error);
        [self.loginView.activityView stopAnimating];
        [self showAlertWithTitle:@"网络异常！"];
    }];
}


- (void)setUI {

    self.loginView = [[LoginView alloc] initWithFrame:self.view.bounds];
    self.loginView.backgroundColor = [UIColor whiteColor];
    self.loginView.userName.delegate = self;
    self.loginView.userPassword.delegate = self;
    
    
#pragma mark -- 登录按钮
    YXWeakSelf
    [self.loginView setLoginClicked:^{
 
        //登录操作
        [weakSelf loadDataLogin];

    }];
    
    [self.view addSubview:self.loginView];
#pragma mark -- 动态登录
    [self.loginView setDynamicLoginClicked:^{
        [weakSelf showAlertWith:@"该功能尚未开放！"];
    }];

#pragma mark -- 忘记密码
    [self.loginView setForgetPasswordClicked:^{
       
        DynamicLoginViewController *dynameicLoginVC = [[DynamicLoginViewController alloc] init];
        [weakSelf.navigationController pushViewController:dynameicLoginVC animated:YES];
    }];
    
#pragma mark -- 我要入驻按钮
    [self.loginView setEnterClicked:^{
       
        BsTenantsViewController *bsTenantsVC = [[BsTenantsViewController alloc] init];
        [weakSelf.navigationController pushViewController:bsTenantsVC animated:YES];

    }];
    
    
    
}



#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //点击return按钮时会触发

    /*
     回收键盘:
     UITextField的对象当被点击时成为第一响应者,其成为第一响应者的标志是弹出键盘.所以要做到回收键盘,只需让UITextField的对象放弃第一响应者即可
     1.点击return回收键盘:UITextField的协议中有一个方法当点击键盘上的return键时该方法会被触发,所以只需在该方法中让对应的UITextField的对象放弃第一响应者即可
     2.点击空白回收键盘:
     */
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //点击空白 回收键盘
    [self.loginView.userPassword resignFirstResponder];
    [self.loginView.userName resignFirstResponder];
}

@end
